import java.net.InetAddress;

/**
 * Project: SecuritySnort
 * Apparently java.net.InetAdress are not usually comparable...
 * so to make them work in a binary try I am going to wrap them in this
 * class to make them comparable.
 *
 * @author Jonathan Buttner
 * @version Mar 5, 2010
 */
public class InetAddComp implements Comparable < InetAddComp >
{
	/**************************************************************************
	 * 							Globals
	 *************************************************************************/
	private InetAddress _ip;
	private String _strIP;
	private String _mac;
	/**************************************************************************
	 * 							Constructors
	 *************************************************************************/
	/**
	 * This constructor sets up this class.
	 * 
	 * @param ip is the underlying object that this class wraps.
	 * @param mac is the mac address in string representation.
	 */
	public InetAddComp( InetAddress ip, String mac )
	{
		_ip = ip;
		_mac = mac;
		_strIP = "";
	}
	
	/**
	 * An ARP packet does not return an InetAddress, so just in case
	 * just ask for a string representation of the IP.
	 * 
	 * @param ip is a string representation of the IP Address.
	 * @param mac is a string representation of the MAC Address.
	 */
	public InetAddComp( String ip, String mac )
	{
		_ip = null;
		_strIP = ip;
		_mac = mac;
	}
	
	/**************************************************************************
	 * 							Getters/Setters
	 *************************************************************************/
	/**
	 * This method returns the underlying InetAddress object.
	 * 
	 * @return _ip is a InetAddress.
	 */
	public InetAddress getInet()
	{
		return _ip;
	}
	
	/**
	 * This method returns the mac address.
	 * 
	 * @return _mac is the MAC Address stored as a string.
	 */
	public String getMAC()
	{
		return _mac;
	}
	
	/**
	 * This method sets the MAC Address.
	 * 
	 * @param mac is the new mac address.
	 */
	public void setMAC( String mac )
	{
		_mac = mac;
	}
	
	/**
	 * This method returns the IP Address.
	 * 
	 * @return a string representation of the IP Address.
	 */
	public String getIP()
	{
		if ( _ip != null )
			return _ip.getHostAddress();
		else if ( !_strIP.isEmpty() )
			return _strIP;
		else
			return "";
	}
	/**************************************************************************
	 * 							Public Methods
	 *************************************************************************/
	/**
	 * This method makes InetAddress comparable. It takes the textual
	 * representation of the IP Address and compares them.
	 * 
	 * @param ip is the object to be compared to.
	 * @return an Integer representing whether the compared object is
	 * less than, great than, or equal.
	 */
	public int compareTo( InetAddComp ip )
	{
		String compIP = ip.getInet().getHostAddress();
		String ipTest = _ip.getHostAddress();
		return ipTest.compareTo( compIP );
	}
	
	/**
	 * This method returns a text representation of the IP Address.
	 * 
	 * @return a string representing the IP Address.
	 */
	public String toString()
	{
		if ( _ip != null )
			return _ip.getHostAddress() + "\t" + _mac;
		else
			return _strIP + "\t" + _mac;
	}
}
